package com.shazhu.api.nongjt;

import com.shazhu.common.core.domain.AjaxResult;
import com.shazhu.common.core.domain.entity.SysDictData;
import com.shazhu.common.utils.StringUtils;
import com.shazhu.common.utils.file.FileUploadUtils;
import com.shazhu.common.utils.nongjt.QiniuUtils;
import com.shazhu.system.service.ISysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "通⽤API")
@RestController
@RequestMapping("/api/common")
public class NjtCommonAPI {
    @Autowired
    private ISysDictTypeService dictTypeService;
    /**
     * 根据字典类型查询字典数据信息
     */
    @Operation(summary = "根据字典类型查询字典列表")
    @GetMapping(value = "/queryDictType")
    public AjaxResult queryDictType(@RequestParam("dictType") String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            return AjaxResult.error("查询失败");
        }
        return AjaxResult.success(data);
    }
    @Autowired
    private QiniuUtils qiniuUtils;

    /**
     * 文件上传
     */
    @Operation(summary = "文件上传到七牛云")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) {
        String uploadPath = qiniuUtils.upload(file, FileUploadUtils.getFileName(file));
        return uploadPath == null ? AjaxResult.error("上传失败") : AjaxResult.success(uploadPath);
    }
}